package com.oauth.google.server.shared.orkut;

import java.util.ArrayList;
import java.util.List;

import com.google.orkut.client.api.BirthdayNotificationEntry;
import com.google.orkut.client.api.BirthdayNotificationTx;
import com.google.orkut.client.api.GetFriendTx;
import com.google.orkut.client.api.GetScrapsTx;
import com.google.orkut.client.api.OrkutPerson;
import com.google.orkut.client.api.ScrapEntry;
import com.oauth.google.client.beans.orkut.OrkutBirthday;
import com.oauth.google.client.beans.orkut.OrkutBirthdayDetails;
import com.oauth.google.client.beans.orkut.OrkutFriendDetails;
import com.oauth.google.client.beans.orkut.OrkutScrap;
import com.oauth.google.client.beans.orkut.OrkutScrapDetails;
import com.oauth.google.client.beans.orkut.OrkutUser;
import com.oauth.service.global.beans.Birthday;
import com.oauth.service.global.beans.Dob;

public class OrkutUtil {

	private static final String ORKUT_BASE_URL = "http://www.orkut.com";

	public static OrkutScrapDetails getOrkutScraps(GetScrapsTx pScrapTx) {
		OrkutScrapDetails scrapDetails = new OrkutScrapDetails();
		int count = pScrapTx.getScrapCount();
		scrapDetails.setCount(count);

		count = count > 20 ? 20 : count;
		List<OrkutScrap> scraps = new ArrayList<OrkutScrap>();
		for (int i = 0; i < count; i++) {
			scraps.add(getOrkutScrap(pScrapTx.getScrap(i)));
		}
		scrapDetails.setScraps(scraps);
		return scrapDetails;
	}

	public static OrkutScrap getOrkutScrap(ScrapEntry pScrapEntry) {
		OrkutScrap scrap = new OrkutScrap();
		scrap.setUser(getOrkutUser(pScrapEntry.getFromUserProfile()));
		scrap.setBody(pScrapEntry.getBody());
		return scrap;
	}

	public static OrkutUser getOrkutUser(OrkutPerson person) {
		OrkutUser userInfo = new OrkutUser();
		if (person != null) {
			userInfo.setThumbNailUrl(person.getThumbnailUrl());
			userInfo.setDisplayName(person.getDisplayName());
			userInfo.setId(person.getId());
			userInfo.setStatus(person.getStatus());
			userInfo.setProfileUrl(person.getProfileUrl());
		}
		return userInfo;
	}

	public static OrkutBirthdayDetails getBirthdayNotifications(
			BirthdayNotificationTx pBirthdayNotification) {
		OrkutBirthdayDetails birthdayDetails = new OrkutBirthdayDetails();
		int count = pBirthdayNotification.getBirthdayNotificationCount();
		birthdayDetails.setCount(count);

		count = count > 50 ? 50 : count;

		List<OrkutBirthday> birthdays = new ArrayList<OrkutBirthday>();
		for (int i = 0; i < count; i++) {
			birthdays.add(getOrkutBirthday(pBirthdayNotification.getBirthdayNotification(i)));
		}
		birthdayDetails.setBirthdays(birthdays);
		return birthdayDetails;

	}

	private static OrkutBirthday getOrkutBirthday(BirthdayNotificationEntry pBirthdayNotification) {
		OrkutBirthday birthday = new OrkutBirthday();
		birthday.setDay(pBirthdayNotification.getBirthDay());
		birthday.setMonth(pBirthdayNotification.getBirthMonth());
		birthday.setUser(getOrkutUser(pBirthdayNotification.getUserProfile()));
		return birthday;
	}

	public static OrkutFriendDetails getOrkutFriends(GetFriendTx pGetFriendsTx) {
		OrkutFriendDetails friendDetails = new OrkutFriendDetails();
		int count = pGetFriendsTx.getFriendsCount();
		friendDetails.setCount(count);

		count = count > 100 ? 100 : count;
		List<OrkutUser> friends = new ArrayList<OrkutUser>(count);

		for (int i = 0; i < count; i++) {
			friends.add(getOrkutUser(pGetFriendsTx.getFriend(i)));
		}
		friendDetails.setFriends(friends);

		return friendDetails;
	}

	public static List<Birthday> getGlobalBirthdays(OrkutBirthdayDetails pBirthdayList) {
		List<Birthday> list = new ArrayList<Birthday>();
		if (pBirthdayList != null && pBirthdayList.getBirthdays() != null) {
			for (OrkutBirthday orkut : pBirthdayList.getBirthdays()) {
				Birthday birthday = new Birthday();
				birthday.setDisplayName(orkut.getUser().getDisplayName());
				birthday.setPictureUrl(orkut.getUser().getThumbNailUrl());
				birthday.setProfileUrl(ORKUT_BASE_URL + orkut.getUser().getProfileUrl());
				birthday.setDob(new Dob(orkut.getDay(), orkut.getMonth()));
				list.add(birthday);
			}
		}
		return list;
	}

}
